package com.jay.beiange.efccp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements OnClickListener {
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments;

    private FragmentManager fm;
    private FragmentTransaction ft;

    private LinearLayout mHome,mProduct,mCase,mUser;
    private ImageButton mHomeImg,mProductImg,mCaseImg,mUserImg;
    private TextView mHomeTitle,mProductTitle,mCaseTitle,mUserTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        initEvent();
        setSelect(0);


    }



    private void initView() {

        mHome = (LinearLayout) findViewById(R.id.id_bottom_tab_home);
        mHomeImg = (ImageButton) findViewById(R.id.id_bottom_tab_home_img);
        mHomeTitle = (TextView) findViewById(R.id.id_bottom_tab_home_title);

        mProduct = (LinearLayout) findViewById(R.id.id_bottom_tab_product);
        mProductImg = (ImageButton) findViewById(R.id.id_bottom_tab_product_img);
        mProductTitle = (TextView) findViewById(R.id.id_bottom_tab_product_title);

        mCase = (LinearLayout) findViewById(R.id.id_bottom_tab_case);
        mCaseImg = (ImageButton) findViewById(R.id.id_bottom_tab_case_img);
        mCaseTitle = (TextView) findViewById(R.id.id_bottom_tab_case_title);

        mUser = (LinearLayout) findViewById(R.id.id_bottom_tab_user);
        mUserImg = (ImageButton) findViewById(R.id.id_bottom_tab_user_img);
        mUserTitle = (TextView) findViewById(R.id.id_bottom_tab_user_title);

        mViewPager = (ViewPager) findViewById(R.id.id_main_viewpager);


        mFragments = new ArrayList<Fragment>();
        Fragment mTab01 = new MainFragment();
        Fragment mTab02 = new ProductFragment();
        Fragment mTab03 = new CaseFragment();
        Fragment mTab04 = new UserFragment();
        mFragments.add(mTab01);
        mFragments.add(mTab02);
        mFragments.add(mTab03);
        mFragments.add(mTab04);

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
        {

            @Override
            public int getCount()
            {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int arg0)
            {
                return mFragments.get(arg0);
            }
        };

        mViewPager.setAdapter(mAdapter);

        mViewPager.setOnPageChangeListener(new OnPageChangeListener()
        {



            @Override
            public void onPageSelected(int arg0)
            {
                int currentItem = mViewPager.getCurrentItem();
                setTab(currentItem);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub

            }
        });
        mViewPager.setCurrentItem(0);

    }
    private void initEvent() {
        mHome.setOnClickListener(this);
        mProduct.setOnClickListener(this);
        mCase.setOnClickListener(this);
        mUser.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
  
        resetImg();

        switch (view.getId())
        {
            case R.id.id_bottom_tab_home:
                setSelect(0);
                mHomeImg.setSelected(true);
                mHomeTitle.setSelected(true);
                break;
            case R.id.id_bottom_tab_product:
                setSelect(1);
                mProductImg.setSelected(true);
                mProductTitle.setSelected(true);
                break;
            case R.id.id_bottom_tab_case:
                setSelect(2);
                mCaseImg.setSelected(true);
                mCaseTitle.setSelected(true);
                break;
            case R.id.id_bottom_tab_user:
                setSelect(3);
                mUserImg.setSelected(true);
                mUserTitle.setSelected(true);
                break;
        }
    }


    private void setSelect(int i)
    {
        setTab(i);
        mViewPager.setCurrentItem(i);
    }

    private void resetImg() {
        mHomeImg.setSelected(false);
        mHomeTitle.setSelected(false);
        mProductImg.setSelected(false);
        mProductTitle.setSelected(false);
        mCaseImg.setSelected(false);
        mCaseTitle.setSelected(false);
        mUserImg.setSelected(false);
        mUserTitle.setSelected(false);

    }

    private void setTab(int i)
    {
        resetImg();
        switch (i)
        {
            case 0:
                mHomeImg.setSelected(true);
                mHomeTitle.setSelected(true);
                break;
            case 1:
                mProductImg.setSelected(true);
                mProductTitle.setSelected(true);
                break;
            case 2:
                mCaseImg.setSelected(true);
                mCaseTitle.setSelected(true);
                break;
            case 3:
                mUserImg.setSelected(true);
                mUserTitle.setSelected(true);
                break;
        }
    }
}
